package com.topone.controller.aspect;

import com.topone.Utils.wx.builder.template.WxTemplateMessageService;
import com.topone.Utils.wx.config.WechatMpProperties;
import com.topone.service.OrderDetailService;
import com.topone.service.OrderService;
import com.topone.service.TcCouponService;
import com.topone.service.TcUserService;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class BaseAspect {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    protected WxMpService wxService;
    protected WxTemplateMessageService wxTemplateMessageService;
    protected WechatMpProperties wechatMpProperties;
    protected TcUserService userService;
    protected OrderDetailService orderDetailService;
    protected TcCouponService tcCouponService;
    protected OrderService orderService;

    @Autowired
    public void setWxTemplateMessageService(WxTemplateMessageService wxTemplateMessageService) {
        this.wxTemplateMessageService = wxTemplateMessageService;
    }

    @Autowired
    public void setWechatMpProperties(WechatMpProperties wechatMpProperties) {
        this.wechatMpProperties = wechatMpProperties;
    }

    @Autowired
    public void setWxService(WxMpService wxService) {
        this.wxService = wxService;
    }

    @Autowired
    public void setOrderDetailService(OrderDetailService orderDetailService) {
        this.orderDetailService = orderDetailService;
    }

    @Autowired
    public void setOrderDetailService(OrderService orderService) {
        this.orderService = orderService;
    }

    @Autowired
    public void setTcCouponService(TcCouponService tcCouponService) {
        this.tcCouponService = tcCouponService;
    }

    @Autowired
    public void setUserService(TcUserService userService) {
        this.userService = userService;
    }


//
//    protected String getCallbackUrl(String url) {
//        return String.format(CALLBACK_USERINFO_URL,
//                wechatMpProperties.getAppId(),
//                wechatMpProperties.getWxRedirectDomain() + url);
//    }

    protected void sendTemplateMessage(WxMpTemplateMessage message) {
        try {
            wxService.getTemplateMsgService().sendTemplateMsg(message);
            LOG.info("Send template message successfully, to: {}", message.getToUser());
            LOG.info("Template message: {}", message.toJson());
        } catch (WxErrorException ex) {
            LOG.error("Send template message error: {}, {}", message.toJson(), ex.fillInStackTrace());
        }
    }
}
